package com.itgungnir.diyshuo.fragment.fenlei_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.itgungnir.diyshuo.activity.R;
import com.itgungnir.diyshuo.base.BaseFragment;

/**
 * 展示“分类”页面的Fragment
 */
public class FenLeiFragment extends BaseFragment implements FenleiContract.View {
    private RadioGroup rg;
    private ImageView search;
    private ViewPager vp;

    private FenleiPresenter presenter = new FenleiPresenter(this);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        rg = (RadioGroup) view.findViewById(R.id.find_fenleimain_rg_rg);
        search = (ImageView) view.findViewById(R.id.find_fenleimain_iv_search);
        vp = (ViewPager) view.findViewById(R.id.find_fenleimain_vp_vp);
        initViews();
        initEvents();
    }

    /**
     * 初始化布局控件
     */
    private void initViews() {
        // 为ViewPager适配数据
        initDataForViewPager();
    }

    /**
     * 初始化控件的监听事件
     */
    private void initEvents() {
        // 当RadioGroup中的选中项发生改变时触发的事件
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                onRadioGroupCheckedChangeListener(checkedId);
            }
        });
        // 当ViewPager滑动时触发的事件
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                onViewPagerPageChangedListener(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        // 点击查找按钮触发的事件
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSearchClickedListener();
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_fenlei_main;
    }

    @Override
    public void initDataForViewPager() {
        presenter.initDataForViewPager(FenLeiFragment.this, vp);
    }

    @Override
    public void onRadioGroupCheckedChangeListener(int checkedId) {
        presenter.onRadioGroupCheckedChangeListener(checkedId, vp);
    }

    @Override
    public void onViewPagerPageChangedListener(int position) {
        presenter.onViewPagerPageChangedListener(position, rg);
    }

    @Override
    public void onSearchClickedListener() {
        presenter.onSearchClickedListener();
    }
}